<template>
  <div class="company-container">
    <div class="app">
      <el-card>
        <el-form
          :model="queryData"
          ref="form"
          :rules="rules"
          
          :inline="true"
          size="normal"
        >
          <el-form-item label="标签名称" label-width="60px">
            <el-input v-model="queryData.tags"></el-input>
          </el-form-item>
          <el-form-item label="城市" prop="province" label-width="45px">
            <el-select
              v-model="queryData.province"
              @change="changeCity"
              placeholder="请选择"
              style="width: 202px"
            >
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item"
                :value="item"
              >
              </el-option
            ></el-select>
          </el-form-item>
          <el-form-item label="地区" prop="city" label-width="45px">
            <el-select
              placeholder="请选择"
              style="width: 202px"
              v-model="queryData.city"
            >
              <el-option
                v-for="item in capital"
                :key="item.id"
                :label="item"
                :value="item"
              >
              </el-option
            ></el-select>
          </el-form-item>
          <el-form-item label="企业简称" label-width="60px">
            <el-input v-model="queryData.shortName"></el-input>
          </el-form-item>
          <el-form-item label="状态" label-width="60px">
            <el-select v-model="queryData.state" placeholder="请选择" style="width: 202px">
              <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="initData"
              >搜索</el-button
            >
            <el-button size="small" @click="clearFn">清除</el-button>
          </el-form-item>

           <el-form-item>
            <el-button size="small"
            class="el-icon-edit" @click="showDialog = true">新增试题</el-button>
          </el-form-item>
        </el-form>

        <!-- ////////////////////////// -->
         <el-alert
          :title="`共计${counts}条`"
          type="info"
          show-icon
          :closable="false"          
        >
        </el-alert>
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column fixed prop="id" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="number" label="企业编号" width="120">
          </el-table-column>
          <el-table-column prop="shortName" label="企业简称" width="120">
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="120">
          </el-table-column>
          <el-table-column prop="creatorID" label="创建者" width="120">
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="120">
          </el-table-column>
          <el-table-column prop="remarks" label="备注" width="120">
          </el-table-column>
          <el-table-column prop="state" label="状态" width="100">
            <template slot-scope="{row}">
              <span v-if="row.state === 1">启用</span>
              <span v-if="row.state === 0">禁用</span>
            </template>
          </el-table-column>

          <el-table-column fixed="right" label="操作" width="180">
            <template slot-scope="{ row }">
          <el-button type="primary" icon="el-icon-edit" circle @click="editFn(row.id)"></el-button>
          <el-tooltip class="item" effect="dark" content="禁用" placement="top-start">
           <el-button type="warning" @click="changeStateFn(row.id, row.state)" v-if="row.state === 1" plain icon="el-icon-close" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="启用" placement="top-start">
           <el-button type="success" @click="changeStateFn(row.id, row.state)" v-if="row.state === 0"  icon="el-icon-check" circle></el-button>
          </el-tooltip>
           <el-button type="danger" @click="delFn(row.id)" icon="el-icon-delete" circle></el-button>
        </template>
        </el-table-column>
        </el-table> 
      </el-card> 


       <!-- 分页组件 -->
    <!-- 3个变量：每页数量、页码数、总数  -->
    <!-- 2个事件：页码切换事件、每页数量切换事件-->
    <el-pagination
      align="right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.page"
      :page-sizes="[3, 4, 5, 10]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts"
    >
    </el-pagination>


      <!-- 弹框 -->
      <el-dialog
        title="新增/编辑"
        :visible.sync="showDialog"
        width="60%"
        @close="close">
        <el-form :model="formData" ref="form" label-width="80px" :inline="false" size="normal">
          <el-form-item label="企业名称"  prop="shortName">
            <el-input v-model="formData.shortName"></el-input>
          </el-form-item>

            <el-form-item label="所属公司" prop="company">
            <el-input v-model="formData.company"></el-input>
          </el-form-item>

          <el-form-item label="城市地区" prop="province" style="marginBottom:0;">
            <el-select
              v-model="formData.province"
              @change="changeCity"
              placeholder="请选择"
              style="width: 202px"
            >
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item"
                :value="item"
              >
              </el-option
            ></el-select>
          </el-form-item>

            <el-form-item  prop="city">
            <el-select
              placeholder="请选择"
              style="width: 202px"
              v-model="formData.city"
            >
              <el-option
                v-for="item in capital"
                :key="item.id"
                :label="item"
                :value="item"
              >
              </el-option
            ></el-select>
          </el-form-item>

           <el-form-item label="方向" prop="tags">
            <el-input v-model="formData.tags"></el-input>
          </el-form-item>

           <el-form-item label="备注" prop="remarks">
            <el-input v-model="formData.remarks"></el-input>
          </el-form-item>
          

          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
  
      </el-dialog>
      
    </div>
  </div>
</template>

<script>
import { provinces, citys } from "@/filters/chengshi.js";
import {getCompanysListApi, addCompanyApi, editCompanyApi,  editSuccessApi , changeStateApi, deleteApi} from  '@/api/companys'
export default {
  name: "CompanysPage",
  data(){
    return{
      formData:{
       isFamous:true,
       creatorID:'',
       number:'',
       state:'',
       addDate:'',
       id:'',
       city:'',
       company:'',
       province:'',
       remarks:'',
       shortName:'',
       tags:''
      },
      showDialog:false,
      stateList:[
        { value: "0",
          label:"禁用"},
          { value: "1",
          label:"启用"}
      ],

      // 初始数据
      queryData:{
        page:1,
        pagesize:10,
      },
      tableData:[],
      counts:''
       
    }
  },
  created(){
    this.initData()


    // 获取城市
    this.getList()
  },

 methods:{
    
    async initData(){
      const {items, counts} = await getCompanysListApi(this.queryData)
      this.tableData = items
      this.counts = counts
      
    },

    getList(){
      this.city = provinces()
    },
    appList(city){
      this.capital = citys(city)
    },
    changeCity(city){
      this.appList(city)
    },

   async onSubmit(){
   
    if(this.formData.id && this.formData.isFamous == 1){
      await editSuccessApi ({
        ...this.formData,
        isFamous:true})
        this.$message.success('修改成功')
    }else if(this.formData.id && this.formData.isFamous == 0){
       await editSuccessApi ({
        ...this.formData,
        isFamous:false})
         this.$message.success('修改成功')
    } else {
      try {
      await addCompanyApi(this.formData)
       this.$message.success('新增成功')
    } catch (error) {
     return console.log(error);
    }
    }
    this.showDialog = false
    this.formData = {isFamous:true}
    },

  async editFn(val){
      console.log(val)
      this.showDialog = true
    const res  = await editCompanyApi(val)
    console.log(res, '----------------][[[[[[[[[[');
    this.formData = res
    },

// 是否启用
 async changeStateFn(id, state){
  if(state === 0){
     await this.$confirm('已成功启用，是否继续？')
     await changeStateApi({
      id,
      state:1
     })
     this.tableData.state = 1
     this.initData()
  } else{
    await this.$confirm('已成功启用，是否继续？')
     await changeStateApi({
      id,
      state:0
     })
       this.tableData.state = 0
     this.initData()
  }

 },

 // 删除
 async delFn(id){
   await this.$confirm('确定要删除吗？')
   try {
     await deleteApi(id)
   } catch (error) {
    return console.log(error);
   }
  this.$message.success('删除成功')
  this.initData()
 },

// 清除
 clearFn(){
  this.initData()
  this.queryData ={}
 },

      // 每页数量切换事件【列表功能】
    handleSizeChange(val) {
      // val是最新的每页数量
      console.log(`每页 ${val} 条`);
      this.queryData.pagesize = val;
      this.$nextTick(() => {
        this.initData();
      });
    },
    //页码切换事件【列表功能】
    handleCurrentChange(val) {
      // val是最新的页码
      console.log(`当前页: ${val}`);
      this.queryData.page = val;
      this.initData();
    },
 }
};
</script>

<style>
.el-form-item__label {
  font-size: 12px;
}

.el-dialog__header {
  background: #409eff
}
</style>
